package com.amado.course07Array.d01dublle;
/**
 *  冒泡排序
 *  冒泡排序是一种简单的排序算法。它重复地走访过要排序的数列，一次比较两个元素，
 *  如果他们的顺序错误就把他们交换过来。走访数列的工作是重复地进行直到没有再需要交换，
 *  也就是说该数列已经排序完成。这个算法的名字由来是因为越小的元素会经由交换慢慢“浮”到数列的顶端。
 * @author Administrator
 *
 */
public class BubbleSort {
	public static void main(String[] args) {
		int[] arr = { 12, 23, 78, 56, 51, 90, 9 };
		for (int i = 0; i < arr.length - 1; i++) {
			for (int j = 0; j < arr.length - i - 1; j++) {// -1为了防止溢出
				//array[0]>array[1]
				//j=0,{ 12, 23, 78, 56, 51, 90, 9 }
				//j=1,{ 12, 23, 78, 56, 51, 90, 9 }
				//j=2,{ 12, 23, 78, 56, 51, 90, 9 }->{ 12, 23, 56, 78, 51, 90, 9 }
				//j=3,{ 12, 23, 56, 78, 51, 90, 9 }->{ 12, 23, 56, 51, 78, 90, 9 }
				//j=4 { 12, 23, 56, 51, 78, 90, 9 }->{ 12, 23, 56, 51, 78, 90, 9 }
				//j=5 { 12, 23, 56, 51, 78, 90, 9 }->{ 12, 23, 56, 51, 78, 9, 90 }
			
				
				//i=1,j<array.length-1-1==> j<5
				//j=0,{ 12, 23, 56, 51, 78, 9, 90 };
				//j=1,{ 12, 23, 56, 51, 78, 9, 90 };
				//j=2,{ 12, 23, 56, 51, 78, 9, 90 }->{ 12, 23, 51, 56, 78, 9, 90 }
				//j=3,{ 12, 23, 51, 56, 78, 9, 90 }
				//j=4,{ 12, 23, 51, 56, 78, 9, 90 }->{ 12, 23, 51, 56, 9, 78, 90 }
				
				//i=2,j<array.length-1-1==> j<4
				//j=0,{ 12, 23, 51, 56, 9, 78, 90 }
				//j=1,{ 12, 23, 51, 56, 9, 78, 90 }
				//j=2,{ 12, 23, 51, 56, 9, 78, 90 }
				//j=3,{ 12, 23, 51, 56, 9, 78, 90 }->{ 12, 23, 51, 9, 56, 78, 90 }
				
				//i=3,j<array.length-1-1==> j<3
				//j=0,{ 12, 23, 51, 9, 56, 78, 90 }
				//j=1,{ 12, 23, 51, 9, 56, 78, 90 }
				//j=2,{ 12, 23, 51, 9, 56, 78, 90 }->{ 12, 23, 9, 51, 56, 78, 90 }
				
				//i=4,j<array.length-1-1==> j<2
				//j=0,{ 12, 23, 9, 51, 56, 78, 90 }
				//j=1,{ 12, 23, 9, 51, 56, 78, 90 }->{ 12, 9, 23, 51, 56, 78, 90 }
				
				//i=5,j<array.length-1-1==> j<1
				//j=0,{ 12, 9, 23, 51, 56, 78, 90 }->{ 9, 12, 23, 51, 56, 78, 90 }
				if (arr[j] > arr[j + 1]) {//交换两数位置
					int temp = arr[j];
					arr[j] = arr[j + 1];
					arr[j + 1] = temp;
				}
			}
		}
		for (int i = 0; i < arr.length; i++) {
			System.out.print(arr[i]+" ");
		}
	}
}
